﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace MapEditor
{
    // for saving 
    class GameObject
    {
        public static Size ObjectDimension(int itemtype)
        {
            int width_, height_;
            int t = Config.ItemIndexSpace;
            switch (itemtype)
            {
                case 0: //mario
                    width_ = 60;
                    height_ = 85;
                    break;
                case 7://goomba
                    width_ = 41;
                    height_ = 53;
                    break;
                case 8://turtle
                    width_ = 60;
                    height_ = 78;
                    break;
                case 9: //tube head
                    width_ = 80;
                    height_ = 26;
                    break;
                case 10: //tube body
                    width_ = 66;
                    height_ = 47;
                    break;


                //tree: Config.ItemIndexSpace
                case 100:
                    width_ = 66;
                    height_ = 152;
                    break;

                //flower: Config.ItemIndexSpace + 1
                case 101:
                    width_ = 120;
                    height_ = 59; break;

                //cloud: Config.ItemIndexSpace + 2
                case 102:
                    width_ = 167;
                    height_ = 67; break;

                //default: 1,2,3,4,5,6,7
                default: width_ = height_ = 45;
                    break;
            }
            return new Size(width_, height_);
        }

        const int moving_area = 1000;
        static int staticID = 0;
        private int _id;

        private int _itemType;
        private Rectangle _bound;
        private Rectangle _moving_bound;
        private bool _upperLayer;
        private int _centerX, _centerY;

        public GameObject(GameObject obj)
        {
            this._id = obj._id;
            this._itemType = obj._itemType;
            this._bound = obj._bound;
            this._moving_bound = obj._moving_bound;
            this._upperLayer = obj._upperLayer;
            this._centerX = obj._centerX;
            this._centerY = obj._centerY;
        }

        public GameObject(int ItemType, Rectangle Bound)
        {
            _id = ++staticID;
            _itemType = ItemType;
            _bound = Bound;

            if (isMovingObj())
            {
                //top-left edge
                int tempX = _bound.X - moving_area / 2 + _bound.Width / 2,
                    tempY = _bound.Y - moving_area / 2 + _bound.Height / 2;
               
                if (tempX < 0) tempX = 0;
                if (tempY < 0) tempY = 0;
                _moving_bound = new Rectangle(tempX, tempY,
                                    moving_area,
                                    moving_area);
            }
            else
            {
                _moving_bound = _bound;
                //just print moving bound's width on file
                if (_moving_bound.Width < _moving_bound.Height)
                    _moving_bound.Width = _moving_bound.Height;
            }

            if (_itemType < Config.ItemIndexSpace) _upperLayer = true;
            else _upperLayer = false;

            _centerX = _bound.X + _bound.Width / 2;
            _centerY = _bound.Y + _bound.Height / 2;
        }
        public Point CenterPosition
        {
            get { return new Point(_centerX, _centerY); }
            set
            {
                _centerX = value.X;
                _centerY = value.Y;
            }
        }
        public int XUpper
        {
            get { return _bound.X; }
            set
            {
                _bound.X = value;
                _centerX = _bound.X + Width / 2;
            }
        }
        public int YUpper
        {
            get { return _bound.Y; }
            set
            {
                _bound.Y = value;
                _centerY = _bound.Y + Height / 2;
            }
        }
        public int Width
        {
            get { return _bound.Width; }
        }
        public int Height
        {
            get { return _bound.Height; }
        }
        public int ItemIndex
        {
            get { return _itemType; }
            set { _itemType = value; }
        }
        public int ID
        {
            get { return _id; }
        }
        public override string ToString()
        {
            string result = "";
            result = _id.ToString() + " " + _itemType.ToString() + " " + _centerX + " " + _centerY + " " + _moving_bound.Width.ToString();
            return result;
        }
        public Rectangle MovingBound
        {
            get { return _moving_bound; }
        }
        public static void ResetStaticID()
        {
            staticID = 0;
        }
        //for loading Game Obj file
        public static int StaticIDAdd()
        {
            return ++staticID;
        }
        public bool IsUpperItem
        {
            get { return _upperLayer; }
        }

        //static methods
        public static Point ConvertToCenterPoint(Point location, Size size)
        {
            return new Point(location.X + size.Width / 2, location.Y + size.Height / 2);
        }
        public static Point ConvertToUpperPoint(Point centerLocation, Size size)
        {
            return new Point(centerLocation.X - size.Width / 2, centerLocation.Y - size.Height / 2);
        }

        private bool isMovingObj()
        {
            //7: goomba, 8:goomba
            if (_itemType == 7 || _itemType == 8) return true;
            return false;
        }
    };

    //for drawing
    class ObjectDraw
    {
        private Image image;
        private Panel panel;
        private int width, height;

        public ObjectDraw(int index, Panel pnl, bool isUpperLayer)
        {
            //switch layer
            if (isUpperLayer)
                image = Form1.UpperTextureList[index];
            else image = Form1.LowerTextureList[index - Config.ItemIndexSpace];

            this.panel = pnl;
            width = GameObject.ObjectDimension(index).Width;
            height = GameObject.ObjectDimension(index).Height;
        }
        public int Width
        {
            get { return width; }
        }
        public int Height
        {
            get { return height; }
        }

        /**
         * location of mouse (draw center)
         * */
        public void Draw(Point location)
        {
            Graphics g = panel.CreateGraphics();
            g.DrawImage(image, GameObject.ConvertToUpperPoint(location, new Size(width, height)));
            g.Dispose();
        }

    }
}
